package simplescenegraph.math;
public class Vector2f {
	
	public static final Vector2f X_AXIS = new Vector2f(1, 0);
	
	public static final Vector2f Y_AXIS = new Vector2f(0, 1);
	
	public float x, y;
	
	float[] xy;
	
	public Vector2f() {
		x=0;y=0;
	}
	
	public Vector2f(float x, float y) {
		this.x=x;this.y=y;
	}
	
	public Vector2f(Vector2f arg0) {
		this(arg0.x,arg0.y);
	}
	
	public Vector2f(Vector2f p0, Vector2f p1) {
		this(p1.x-p0.x, p1.y-p0.y);
	}
	
	public void add(Vector2f arg0) {
		this.x+=arg0.x;this.y+=arg0.y;
	}
	
	public void subtract(Vector2f arg0) {
		this.x-=arg0.x;this.y-=arg0.y;		
	}
	
	public void multiply(float a) {
		this.x*=a;this.y*=a;
	}
	
	public void divide(float a) {
		this.x/=a;this.y/=a;
	}
	
	public float dotProduct(Vector2f arg0) {
		return x*arg0.x + y*arg0.y;
	}
	
	public Vector2f projection(Vector2f q) {
		Vector2f projQP = new Vector2f(q);
		projQP.multiply((dotProduct(q) / q.squaredLength()));
		return projQP;
	}
	
	public float squaredLength() {
		return x*x + y*y;
	}
	
	public float length() {
		return (float) Math.sqrt(x*x + y*y);
	}
	
	public void normalize() {
		float l0 = length();
		if (l0 > 0) {
		x /= l0;
		y /= l0;
		}
	}
	
	public boolean equals(Object obj0) {
		Vector2f v0 = (Vector2f) obj0;
		return x == v0.x && y == v0.y;
	}
	
	public String toString() {
		return "|" + x + "|\n|" + y + '|'; 
	}
	
	public float[] toFloatArray() {
		if (xy == null) xy = new float[2];
		xy[0] = x; xy[1] = y;
		return xy;
	}
	
}
 
